from rest_framework.generics import *
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from myapp.models import *
# Create your views here.
from django.views import View
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render

from myapp.serializers import *

"""
CBV
"""
# class RegisterView(View):
#
#     def get(self, request):
#         return render(request, 'register.html')
#
#     def post(self, request):
#         username = request.POST.get('username')
#         password = request.POST.get('password')
#         repassword = request.POST.get('repassword')
#         if password == repassword:
#             user = User.objects.create(username=username, password=password)
#             if user:
#                 return HttpResponse('注册成功')
#         return HttpResponse('注册失败')
#
#     def delete(self):
#         pass
#
#     def put(self):
#         pass
#
#
# class UserView(View):
#
#     def get(self, request, uid):
#         user = User.objects.get(pk=uid)
#         return JsonResponse({"status": 200, 'user': user.to_dirct()})
#
#     def post(self, request):
#         username = request.POST.get('username')
#         password = request.POST.get('password')
#         repassword = request.POST.get('repassword')
#         if password == repassword:
#             user = User.objects.create(username=username, password=password)
#             if user:
#                 return JsonResponse({'status': 200, 'msg': '注册成功'})
#
#         return JsonResponse({'status': 200, 'msg': '注册失败'})
#
#     def delete(self):
#         pass
#
#     def put(self):
#         pass


class UserView(APIView):
    """普通方法"""
    def get(self, request):
        pass

    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        phone = request.POST.get('phone')
        user = User.objects.create(username=username, password=password, phone=phone)

        user_serializer = UserSerializer(user)

        return JsonResponse({'status': 200, 'user': user_serializer.data})


class UserView1(APIView):
    """新方法"""
    def get(self, request):
        pass

    def post(self, request):

        user_serializer1 = UserSerializer1(data=request.data)
        if user_serializer1.is_valid():
            user_serializer1.save()
            return JsonResponse({'status': 200, 'user': user_serializer1.data})


class UserTypeView(APIView):
    def get(self, request):
        usertypes = UserType.objects.all()
        serializer = UserTypeSerializer(usertypes, many=True, context={'request': request})  # many=True会将queryset对象转成列表
        data = {
            'status': 200,
            'types': serializer.data
        }
        return JsonResponse(data)


class SingleUserView(APIView):
    def get(self, request, id):
        user = User.objects.get(pk=id)
        serializer = SingleUserSerializer(user,context={'request': request})
        data = {
            'status': 200,
            'types': serializer.data
        }
        return JsonResponse(data)


"""使用APIView的子类一些get或post方法都封装好了"""
class UserRecource(ListCreateAPIView):
    # 操作的模型
    queryset = User.objects.all()
    # 此模型对应的序列化类
    serializer_class = SingleUserSerializer


class UserRecource1(RetrieveUpdateDestroyAPIView):
    queryset = User.objects.all()
    serializer_class = SingleUserSerializer


class UserRecource2(ModelViewSet):  # 继承了所有的方法
    queryset = User.objects.all()
    serializer_class = SingleUserSerializer
